package 多线程;

/**
 * @ClassName: Demo02
 * @Auther: Drawin
 * @Date: 2021/11/18 17:04
 * @Description:11-2
 */

public class Demo02 implements Runnable{
    //实现Runnable接口的方式来建立线程类
    private String name;//用于显示，不是线程的名字
    public Demo02(String name){
        this.name=name;
        System.out.println(name+"创建成功");
    }

    public void run(){
        for (int i = 0; i <3; i++) {
            System.out.println(name+"第"+i+"次运行");
            Thread.yield();//让当前线程让出CPU
        }
    }

    public static void main(String[] args) {
        Demo02 r1=new Demo02("第一个线程");
        Demo02 r2 = new Demo02("第二个线程");
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        System.out.println("t1,t2线程开始启动");
        t1.start();
        t2.start();
        System.out.println("main方法运行完毕");
    }

}
